define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'setting/index' + location.search,
                    add_url: 'setting/add',
                    edit_url: 'setting/edit',
                    del_url: 'setting/del',
                    multi_url: 'setting/multi',
                    table: 'setting',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                searchFormVisible: false,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'sale_amount', title: __('Sale_amount'), operate:'BETWEEN'},
                        // {field: 'parent_sale_amount', title: __('Parent_sale_amount'), operate:'BETWEEN'},
                        // {field: 'warehouse_sale_amount', title: __('Warehouse_sale_amount'), operate:'BETWEEN'},
                        {field: 'shop_ship_amount', title: __('Shop_ship_amount'), operate:'BETWEEN'},
                        {field: 'warehouse_amount', title: __('Warehouse_amount'), operate:'BETWEEN'},
                        {field: 'level_agent_amount', title: __('Level_agent_amount'), operate:'BETWEEN'},
                        {field: 'level_warehouse_amount', title: __('Level_warehouse_amount'), operate:'BETWEEN'},
                        {field: 'promotion_join_number', title: __('Promotion_join_number'), operate:'BETWEEN'},
                        // {field: 'video_file', title: __('Video_file')},
                        {field: 'video_image', title: __('Video_image'), events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'reward_goods_id', title: __('代言人赠送商品'), formatter: function(value, row, index) {
                            return row.goods.name +'【'+row.spec.name+'】';
                        }},
                        {
                            field: 'operate', 
                            title: __('Operate'), 
                            table: table, 
                            buttons: [],
                            events: Table.api.events.operate, 
                            formatter: function (value, row, index) {
                                var that = $.extend({}, this);
                                var table = $(that.table).clone(true);
                                $(table).data("operate-del", null);
                                that.table = table;
                                return Table.api.formatter.operate.call(that, value, row, index);
                            }
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
             $("#c-reward_spec_id").data("params", function (obj) {
                return {custom: {goods_id: $("#c-reward_goods_id").val()}};
            });
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});